###Script to Code variables in Study 2---------------------------

###Clear R------------------------
rm(list=ls())

####function to install packages if they don't exist----------------
ipak <- function(pkg){
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg)) 
    install.packages(new.pkg, dependencies = TRUE)
  sapply(pkg, require, character.only = TRUE)
}

packages <- c("car", "psych","moments", "lavaan", "plyr")
ipak(packages)

###Function to recode variables to range from lowest (0) to highest (1) observation
zero1 <- function(x, minx=NA, maxx=NA){
  res <- NA
  if(is.na(minx)) res <- (x - min(x,na.rm=T))/(max(x,na.rm=T) -min(x,na.rm=T))
  if(!is.na(minx)) res <- (x - minx)/(maxx -minx)
  res
}


#Load data
load("Study2_replication.RData")

### Dependent Variable-----------------
data$DV1 <- as.numeric(factor(data$DV,levels=c("Strongly oppose","Oppose","Somewhat oppose","Neither support nor oppose","Somewhat support","Support","Strongly support")))

### Need for Cognition ------------------
data$nfc_1 <- factor(data$nfc_1,levels(as.factor(data$nfc_1))[c(3,5,6,4,2)])
data$nfc_2 <- factor(data$nfc_2,levels(as.factor(data$nfc_1)))
data$nfc_3 <- factor(data$nfc_3,levels(as.factor(data$nfc_1)))
data$nfc_4 <- factor(data$nfc_4,levels(as.factor(data$nfc_1)))
data$nfc_5 <- factor(data$nfc_5,levels(as.factor(data$nfc_1)))
data$nfc_6 <- factor(data$nfc_6,levels(as.factor(data$nfc_1)))
data$nfc_7 <- factor(data$nfc_7,levels(as.factor(data$nfc_1)))
data$nfc_8 <- factor(data$nfc_8,levels(as.factor(data$nfc_1)))
data$nfc_9 <- factor(data$nfc_9,levels(as.factor(data$nfc_1)))
data$nfc_10 <- factor(data$nfc_10,levels(as.factor(data$nfc_1)))
data$nfc_11 <- factor(data$nfc_11,levels(as.factor(data$nfc_1)))
data$nfc_12 <- factor(data$nfc_12,levels(as.factor(data$nfc_1)))
data$nfc_13 <- factor(data$nfc_13,levels(as.factor(data$nfc_1)))
data$nfc_14 <- factor(data$nfc_14,levels(as.factor(data$nfc_1)))
data$nfc_15 <- factor(data$nfc_15,levels(as.factor(data$nfc_1)))
data$nfc_16 <- factor(data$nfc_16,levels(as.factor(data$nfc_1)))
data$nfc_17 <- factor(data$nfc_17,levels(as.factor(data$nfc_1)))
data$nfc_18 <- factor(data$nfc_18,levels(as.factor(data$nfc_1)))

nfcvars <-(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(nfc_1,nfc_2,nfc_3,nfc_4,nfc_5,nfc_6,nfc_7,nfc_8,nfc_9,nfc_10,nfc_11,nfc_12,nfc_13,nfc_14,nfc_15,nfc_16,nfc_17,nfc_18))))
nfcvars[,c(3,4,5,7,8,9,12,16,17)] <-   1-nfcvars[,c(3,4,5,7,8,9,12,16,17)]
data$nfc <- rowMeans(nfcvars,na.rm=T)

### Cognitive Reflection Test------------------------------
data$CRT1 <- ifelse(data$CRT1 %in% c("5",".05","0,05","0.05"),1,0)
data$CRT2 <- ifelse(data$CRT2 %in% c("5","05","5.00"),1,0)
data$CRT3 <-  ifelse(data$CRT3 %in% c("47"),1,0)
data$CRT4 <- ifelse(data$CRT4 %in% c("4","4 days","4 days.","4days"),1,0)
data$CRT5 <- ifelse(as.numeric(data$CRT5) %in% c("29"),1,0)
data$CRT6 <- ifelse(as.numeric(data$CRT6) %in% c("20"),1,0)
data$CRT7 <- ifelse(as.numeric(as.factor(data$CRT7))==4,1,0)
data$CRTall <- rowSums(with(data,data.frame(CRT1,CRT2,CRT3,CRT4,CRT5,CRT6,CRT7)),na.rm=T)


### Political Knowledge -----------------------
data$pk1 <- data$Q40=='Vice President'
data$pk2 <- data$Q43 =='Speaker of the House of Representatives'
data$pk3 <- data$Q44 =='Angela Merkel'
data$pk4 <- data$Q45 =='Christine Legarde'
data$pk5 <- data$Q46 =='Chief Justice of the Supreme Court'
data$pk6 <- data$Q55 =='School curriculum standards for language and math'
data$pk7 <- data$Q56 =='6 years'
data$pk8 <- data$Q57 =='Benjamin Netanyahu'
data$pk9 <- data$Q58 =='Betsy Devos'
data$pk10 <- data$Q59 =='Tim Kaine'
data$pk <- rowMeans(with(data,data.frame(pk1,pk2,pk3,pk4,pk5,pk6,pk7,pk8,pk9,pk10)))
### Latent cognitive resources-----------------
data$NFC1_rec<-as.numeric(factor(data$nfc_1))
data$NFC2_rec<-as.numeric(factor(data$nfc_2))
data$NFC3_rec<-6-as.numeric(factor(data$nfc_3))
data$NFC4_rec<-6-as.numeric(factor(data$nfc_4))
data$NFC5_rec<-6-as.numeric(factor(data$nfc_5))
data$NFC6_rec<-as.numeric(factor(data$nfc_6))
data$NFC7_rec<-6-as.numeric(factor(data$nfc_7))
data$NFC8_rec<-6-as.numeric(factor(data$nfc_8))
data$NFC9_rec<-6-as.numeric(factor(data$nfc_9))
data$NFC10_rec<-as.numeric(factor(data$nfc_10))
data$NFC11_rec<-as.numeric(factor(data$nfc_11))
data$NFC12_rec<-6-as.numeric(factor(data$nfc_12))
data$NFC13_rec<-as.numeric(factor(data$nfc_13))
data$NFC14_rec<-as.numeric(factor(data$nfc_14))
data$NFC15_rec<-as.numeric(factor(data$nfc_15))
data$NFC16_rec<-6-as.numeric(factor(data$nfc_16))
data$NFC17_rec<-6-as.numeric(factor(data$nfc_17))
data$NFC18_rec<-as.numeric(factor(data$nfc_18))

#subset data for those that have missing values on CRT & NFC
data <- subset(data,CRT1>=0 & CRT2>=0 & CRT3>=0 & CRT4>=0 & CRT5>=0 & CRT6>=0 & CRT7>=0 & NFC1_rec>=1)

psych::alpha(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(NFC1_rec, NFC2_rec, NFC3_rec, NFC4_rec, NFC5_rec, NFC6_rec, NFC7_rec, NFC8_rec, NFC9_rec, NFC10_rec, NFC11_rec, NFC12_rec, NFC13_rec, NFC14_rec, NFC15_rec, NFC16_rec, NFC17_rec, NFC18_rec))),check.keys = T) #alpha is very high

latent <- ' cogresources  =~ NA*+NFC1_rec+ NFC2_rec+ NFC3_rec+ NFC4_rec+ NFC5_rec+ NFC6_rec+ NFC7_rec+ NFC8_rec+ NFC9_rec+ NFC10_rec+ NFC11_rec+ NFC12_rec+ NFC13_rec+ NFC14_rec+ NFC15_rec+ NFC16_rec+ NFC17_rec+ NFC18_rec+CRT1+CRT2+CRT3+CRT4+CRT5+CRT6+CRT7+pk1+pk2+pk3+pk4+pk5+pk6+pk7+pk8+pk9+pk10
# fix variance of latent variable
cogresources ~~ 1*cogresources'

fit <- cfa(latent, ordered=c( "NFC1_rec", "NFC2_rec", "NFC3_rec", "NFC4_rec", "NFC5_rec", "NFC6_rec", "NFC7_rec", "NFC8_rec", "NFC9_rec", "NFC10_rec", "NFC11_rec", "NFC12_rec", "NFC13_rec", "NFC14_rec", "NFC15_rec", "NFC16_rec", "NFC17_rec", "NFC18_rec","CRT1", "CRT2", "CRT3", "CRT4", "CRT5", "CRT6", "CRT7", "pk1", "pk2", "pk3", "pk4", "pk5", "pk6", "pk7", "pk8", "pk9", "pk10"), data=data)

#fit model
data$cogresources<-(lavPredict(fit, type = "lv", method="EBM", label = TRUE, optim.method = "nlminb"))

### Partisan Social Identity Strength-------------------------
dp <-with(data,data.frame(pid1,pid2,pid3,pid3.1,Q19,pid4,pid5,pid6))
dp[dp==""]     = NA
dp <- colwise(droplevels)(dp)
dp <- colwise(function(x)factor(x,levels=c("Strongly disagree","Somewhat disagree","Neither agree nor disagree","Somewhat agree","Strongly agree")))(dp)

dp <- colwise(.fun = function(x)as.numeric(factor(x,levels=c("Strongly disagree","Somewhat disagree","Neither agree nor disagree","Somewhat agree","Strongly agree"))))(dp)
dp <- colwise(as.numeric)( dp)
data$dempid <- rowMeans(dp,na.rm=T)
rp <-with(data,data.frame(Q20,Q21,Q22,Q23,Q24,Q25,Q26.1,Q27.1))
rp[rp==""]     = NA
rp <- colwise(function(x)factor(x,levels=c("Strongly disagree","Somewhat disagree","Neither agree nor disagree","Somewhat agree","Strongly agree")))(rp)
rp <- colwise(as.numeric)( rp)
data$reppid <- rowMeans(rp,na.rm=T)
data$pidstrength <- rowMeans(with(data,data.frame(dempid,reppid)),na.rm=T)

### Code treatment----------------
data$condition <- car::recode(as.numeric(as.factor(data$FarmPolicy_DO)),"2='Control';3='Dems Support';4='Reps Support';else=NA")
data$pid<-ifelse(data$Q31=="Republican" | data$Q34=="Republican party",'Republican', 'Democrat')

data$inpartycue <- 'control'
data$inpartycue[data$condition=='Dems Support' & data$pid=='Democrat']='In Party Cue'
data$inpartycue[data$condition=='Dems Support' & data$pid=='Republican']='Out Party Cue'
data$inpartycue[data$condition=='Reps Support' & data$pid=='Democrat']='Out Party Cue'
data$inpartycue[data$condition=='Reps Support' & data$pid=='Republican']='In Party Cue'

data$cues<-as.numeric(as.factor(data$inpartycue))
data$InParty<-ifelse(data$cues==2,1,0)
data$OutParty<-ifelse(data$cues==3,1,0)


### Covariates-------------
#Education
data$education <- car::recode(as.numeric(as.factor(data$Q29)), "8=1; 6=1;7=2;5=3; 4=3; 3=NA; 2=NA; 1=NA")

#age
data$age<-as.numeric(as.factor(data$Q51))

#Female
data$female<-ifelse(data$Q26=="Female",1,0)

#Race
data$non_white<-ifelse(data$Q27=="White",0,1)

#Party
data$Republican_dummy<-ifelse(data$pid=="Republican",1,0)

#Partisanship
data$partisanship <- 4
data$partisanship[which(data$Q32=="Strong Republican")]=1
data$partisanship[which(data$Q32=="Not very strong Republican")]=2
data$partisanship[which(data$Q34=="Republican Party")]=3
data$partisanship[which(data$Q34=="Democratic party")]=5
data$partisanship[which(data$Q33=="Not very strong Democrat")]=6
data$partisanship[which(data$Q33=="Strong Democrat")]=7

#Partisan identity strength
data$tradition_pid_strength<-zero1(abs(data$partisanship-4))

#partisanship without independents
data$partisanship_ind <- NA
data$partisanship_ind[which(data$Q32=="Strong Republican")]=1
data$partisanship_ind[which(data$Q32=="Not very strong Republican")]=2
data$partisanship_ind[which(data$Q34=="Republican party ")]=3
data$partisanship_ind[which(data$Q34=="Democratic party")]=4
data$partisanship_ind[which(data$Q33=="Not very strong Democrat")]=5
data$partisanship_ind[which(data$Q33=="Strong Democrat")]=6
table(data$partisanship_ind)


#subset data: drop if DV is missing
data <- subset(data,is.na(DV1)==F)

#recode to range from 0-1
data$cogresources<-zero1(data$cogresources)
data$pidstrength <- zero1(data$pidstrength)
data$CRTall<-zero1(data$CRTall)
data$nfc<-zero1(data$nfc)
data$DV1 <- zero1(data$DV1)

#save
save(data, file="Study2_data.RData")
